/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package humanMirror.model.analyzer;

/**
 *
 * @author Admin
 */
public class Knowledge {

    int minValue;
    int maxValue;
    final int minLimit;
    final int maxLimit;
    String opinion;

    public Knowledge(int minValue, int maxValue, String opinion) {
        minLimit = 0;
        maxLimit = 100;
        initializeBorder(minValue, maxValue);
        this.opinion = opinion;
    }

    public void setOpinion(String opinion) {
        this.opinion = opinion;
    }
    
    public boolean isIn(int value) {
        return this.equals(new Knowledge(value, value, opinion));
    }
    
    public boolean isIntersection(Knowledge knowledge) {
        if(this.equals(knowledge)) {
            return true;
        }
        
        if(minValue > knowledge.maxValue) {
           return false;
        }
        
        if(maxValue < knowledge.minValue) {
            return false;
        }
        
        return true;
    }
    
    @Override
    public boolean equals(Object obj) {
       if(this == obj) {
            return true;
        }
        
        if(!(obj instanceof  Knowledge)) {
            return false;
        }
        
        Knowledge knowledge = (Knowledge) obj; 
        
        if(minValue <= knowledge.minValue && maxValue >= knowledge.maxValue)
            return true;
        if(minValue >= knowledge.minValue && maxValue <= knowledge.maxValue)
            return true;
        
        return false;
    }

    @Override
    public String toString() {
        return "minValue: " + minValue + "; maxValue: " + maxValue +
                " Opinion: " + opinion;
    }  
    
    private void initializeBorder(int minValue, int maxValue) {
        checkValue(minValue);
        checkValue(maxValue);
        
        if (minValue > maxValue) {
            throw new IllegalArgumentException("Min mast be not bigger then Max");
        }

        this.minValue = minValue;
        this.maxValue = maxValue;
    }

    private void checkValue(int val) {
        if ((val < minLimit) || (val >  maxLimit)) {
            throw new IllegalArgumentException("val must be > " + minLimit
                    + " but < " +  maxLimit);
        }
    }
}
